#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time    : 2018/4/16 11:20
# @Author  : simple0426
# @Email   : istyle.simple@gmail.com
# @File    : vagrant_ctl.py
# @Software: PyCharm
# @desc    : 此脚本主要使用vagrant控制virtualbox虚拟机

import subprocess
import os, sys

box_list = {
    'haproxy': ['/home/dell/haproxy/', '10.10.10.64'],
    'nginx': ['/home/dell/nginx/', '10.10.10.65'],
    'java01': ['/home/dell/java/java01/', '10.10.10.194'],
    'java02': ['/home/dell/java/java02/', '10.10.10.192'],
    'java03': ['/home/dell/java/java03/', '10.10.10.195'],
    'java04': ['/home/dell/java/java04/', '10.10.10.190'],
    'java05': ['/home/dell/java/java05/', '10.10.10.74'],
    'java06': ['/home/dell/java/java06/', '10.10.10.191'],
    'java07': ['/home/dell/java/java07/', '10.10.10.193'],
    'java08': ['/home/dell/java/java08/', '10.10.10.77'],
    'java09': ['/home/dell/java/java09/', '10.10.10.78'],
    'jenkins': ['/home/dell/jenkins/', '10.10.10.100'],
    'python01': ['/home/dell/python/python01/', '10.10.10.90'],
    'python02': ['/home/dell/python/python02/', '10.10.10.92'],
    'python03': ['/home/dell/python/python03/', '10.10.10.93'],
    'python04': ['/home/dell/python/python04/', '10.10.10.94'],
}

def vagant(ops):
    shell = subprocess.Popen(['vagrant', ops], stdout=subprocess.PIPE)
    output = shell.communicate()[0]
    print(output.decode('utf-8'))

def forloop(ops):
    for host in box_list.keys():
        print("\033[1;32;40m###################%s(%s)######################\033[0m" % (host, box_list[host][1]))
        print("VirtualHostPath: %s\n" % box_list[host][0])
        os.chdir(box_list[host][0])
        vagant(ops)
def forloop1():
    for host in box_list.keys():
        os.chdir(box_list[host][0])
        os.system('vagrant ssh -c "sudo route add default gw 10.10.10.254;sudo route del default gw 10.0.2.2"')

if __name__ == '__main__':
    if len(sys.argv) == 2:
        if sys.argv[1] == 'suspend':
            forloop('suspend')
        elif sys.argv[1] == 'resume':
            forloop('resume')
        elif sys.argv[1] == 'status':
            forloop('status')
        elif sys.argv[1] == 'up':
            forloop('up')
        elif sys.argv[1] == 'route':
            forloop1()
        else:
            print('Usage: %s suspend|resume|status|up|route' % sys.argv[0])
    else:
        print('Usage: %s suspend|resume|status|up|route' % sys.argv[0])
